package com.example.controller;

import com.example.domain.ResponseResult;
import com.example.domain.dto.ChangePasswordDto;
import com.example.domain.dto.ModInitUserDto;
import com.example.domain.entity.User;
import com.example.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    //通过前端传入的字段查询数据库的用户数据信息，有模糊查询等
    @PostMapping("/list")
    @PreAuthorize("@ps.hasPermission('userList')")
    public ResponseResult getUserList(@RequestBody Map<String,String> map){
        return userService.getUserList(map);
    }

    @PostMapping("/delete")
    @PreAuthorize("@ps.hasPermission('userDelete')")
    public ResponseResult deleteUser(@RequestBody Map<String,String> map){
        return userService.deleteUser(map);
    }
    @PostMapping("/modInit")
    @PreAuthorize("@ps.hasPermission('userEdit')")
    public ResponseResult modInitUser(@RequestBody Map<String,String> map){
        return userService.modInitUser(map);
    }

    @PostMapping("/mod")
    @PreAuthorize("@ps.hasPermission('userEdit')")
    public ResponseResult modUser(@Valid @RequestBody User user){
        return userService.modUser(user);
    }

    @PostMapping("/changePassword")
    public ResponseResult changePassword(@Valid @RequestBody ChangePasswordDto changePasswordDto){
        return userService.changePassword(changePasswordDto);
    }

    @PostMapping("/addInit")
    @PreAuthorize("@ps.hasPermission('userAdd')")
    public ResponseResult addInitUser(){
        return userService.addInitUser();
    }

    @PostMapping("/add")
    @PreAuthorize("@ps.hasPermission('userAdd')")
    public ResponseResult addUser(@Valid @RequestBody User user){
        return userService.addUser(user);
    }
}
